package com.example.lesson1;

import java.util.Stack;

/**
 * @author ${user}
 * @version V1.0
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}(用一句话描述该文件做什么)
 * @date ${date}
 */
public class Solution {
    private final static char ADD = '+';
    private final static char SUBTRACT = '-';
    private final static char MULTIPLY = '*';
    private final static char DIVIDE = '/';
    private final static char LEFT_BRACKET = '(';
    private final static char RIGHT_BRACKET = ')';

    public int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[]{i, j};
                }
            }
        }
        return null;
    }

    public int calculate(String s) {
        final Stack<Integer> op = new Stack();
        final Stack<Integer> ss = new Stack();
        int now = 0, sum = 0, flag = 1;
        final char[] result = s.toCharArray();
        for (int i = 0; i < result.length; i++) {
            if (result[i] >= '0' && result[i] <= '9') {
                now = now * 10 + result[i] - '0';
            } else {
                sum += flag * now;
                now = 0;
                if (result[i] == ' ') {
                    continue;
                } else if (result[i] == ADD) {
                    flag = 1;
                } else if (result[i] == SUBTRACT) {
                    flag = -1;
                } else if (result[i] == LEFT_BRACKET) {
                    ss.push(sum);
                    op.push(flag);
                    sum = 0;
                    flag = 1;
                } else {
//                    sum = ss.size() > 1 ? ss.pop() : 0 + sum * (op.size() > 1 ? op.pop() : 0);
                    sum = ss.pop()+ sum * op.pop();
                }
            }
        }
        return sum + now * flag;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        final int[] temp = {1, 2, 3, 4, 5, 6};
        final int[] twoSum = s.twoSum(temp, 6);
        for (int a : twoSum) {
            System.out.println(a);
        }

        System.out.println("===>" + s.calculate("(3+7*2)"));
    }
}
